package com.example.tivi.connection;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.tdx.androidlibs.download.RegularDownload;
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

import com.example.tivi.entity.Category;
import com.example.tivi.entity.Channel;
import com.example.tivi.entity.GlobalInfor;
import com.example.tivi.util.Data;
import com.example.tivi.util.LogUtils;
import com.example.tivi.util.Preferences;

public class DataLoader extends AsyncTaskLoader<Boolean> {
	private static final String TAG = "DataLoader";

	private static final String url = "http://192.168.0.24:8008/TiviChannel.aspx";

	public DataLoader(Context context) {
		super(context);
	}

	@Override
	public Boolean loadInBackground() {
		// lay' thong tin verion url
		try {
			String data = RegularDownload.downloadFile(url + "?cmd=config");
			LogUtils.d(TAG, "length=" + data.length());
			LogUtils.d(TAG, "data=" + data);

			JSONObject jsonObject = new JSONObject(data);
			int version = jsonObject.getInt("version");
			Log.i(TAG, "Version server=" + version);
			Log.i(TAG, "Version local="
					+ Preferences.getInstance(getContext()).getVersionURL());
			if (version > Preferences.getInstance(getContext()).getVersionURL()) {

				String content = RegularDownload.downloadFile(url);
				LogUtils.d(TAG, "content=" + content);

				GlobalInfor.getInstance().setCategoryList(
						getCategoryList(content));
				Preferences.getInstance(getContext()).setVersionURL(version);
				Data.saveData(getContext(), GlobalInfor.getInstance());
			} else {
				// lay thong tin cache
				// TODO get content from local
				// String content = RegularDownload.downloadFile(url);
				// GlobalInfor.mCategoryList = getCategoryList(content);
				GlobalInfor globalInfor = Data.getData(getContext());
				GlobalInfor.getInstance().setCategoryList(
						globalInfor.getCategoryList());
				Log.i(TAG, "Size channel="
						+ GlobalInfor.getInstance().getCategoryList().size());
			}
			LogUtils.d(TAG, data);
			return true;

		} catch (IOException e) {
			e.printStackTrace();
			Log.e(TAG, "can not load data");
		}
		// lay thong tin app promote
		catch (JSONException e) {
			e.printStackTrace();
		}

		return false;
	}

	private List<Category> getCategoryList(String data) {
		List<Category> categoryList = new ArrayList<Category>();
		try {
			JSONObject jsonObject = new JSONObject(data);
			JSONArray jsonArray = jsonObject.getJSONArray("categoryList");

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				String categoryName = json.getString("categoryName");
				int categoryId = json.getInt("categoryId");
				String thumbnail = json.getString("thumbnail");

				List<Channel> channelList = new ArrayList<Channel>();

				JSONArray array = json.getJSONArray("channelList");
				for (int j = 0; j < array.length(); j++) {
					JSONObject jsonObject2 = array.getJSONObject(j);
					int channelId = jsonObject2.getInt("channelId");
					String channelName = jsonObject2.getString("channelName");
					String channelThumbnail = jsonObject2
							.getString("thumbnail");
					String description = jsonObject2.getString("description");
					String urls = jsonObject2.getString("urls");

					String[] strings = urls.split("\\|");
					// String url1 = null,url2 = null,url3 = null;
					// for (int k = 0; k < strings.length; k++) {
					// if(k==0)url1=strings[k];
					// if(k==1)url2=strings[k];
					// if(k==2)url3=strings[k];
					// }
					List<String> list = new ArrayList<String>();
					for (int k = 0; k < strings.length; k++) {
						list.add(strings[k]);
					}
					Channel channel = new Channel(channelId, channelName,
							channelThumbnail, list, description);
					channelList.add(channel);
				}
				Category category = new Category(categoryId, categoryName,
						channelList, thumbnail);
				categoryList.add(category);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return categoryList;
	}

}
